package jdk8demo.interfacefunction;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.IntSummaryStatistics;
import java.util.List;

public class StreamDemo {

	public static void main(String[] args) {
		// parallelStream / stream
		List<String> strs = new ArrayList<>(Arrays.asList("abandon", "john", "candy", "winter", "candy"));
		strs.stream().filter(s -> s.startsWith("a")).forEach(System.out::println);
		System.out.println(strs.stream().filter(s -> s.startsWith("a")).count());
		System.out.println("=============");
		strs.stream().sorted((a, b) -> b.compareTo(a)).forEach(System.out::println);
		System.out.println("=============");
		strs.stream().map(s -> s.substring(1)).sorted((a, b) -> b.compareTo(a)).forEach(System.out::println);
		System.out.println("=============");
		strs.stream().distinct().limit(2).forEach(System.out::println);
		System.out.println("=============");

		// match
		System.out.println(strs.stream().anyMatch(s -> s.startsWith("a")));
		System.out.println(strs.stream().allMatch(s -> s.startsWith("a")));
		System.out.println(strs.stream().noneMatch(s -> s.startsWith("d")));
		// map接收一个参数返回一个结果 reduce 接收两个参数，第⼀一个参数为上⼀一次参与运算的结果，第⼆二个参数为新进⼊入的元素
		// BinaryOperator 传入两个参数返回一个结果
		System.out.println(strs.stream().map(s -> s.substring(0, 1)).reduce((a, b) -> a + "##" + b).get());
		List<Integer> ints = new ArrayList<>(Arrays.asList(1, 5, 4, 3, 2));
		// ToIntFunction<T> 接受一个输入参数,返回一个int类型结果
		IntSummaryStatistics summary = ints.stream().mapToInt(x -> x).summaryStatistics();
		System.out.println(summary.getMax());
		System.out.println(summary.getMin());
		System.out.println(summary.getSum());
		System.out.println(summary.getAverage());
		System.out.println(summary.getCount());
	}
}
